package pnu.ssm.parser;

import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import pnu.ssm.koreanDictionary.KoreanDictionary;
import pnu.ssm.sbchild.ChoiceActivity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class KoreaDictionaryXMLParser implements Runnable {
	// Debugging
	private static final String TAG = "LocalSearchSaxXMLParser";

	private String mUrl = "http://openapi.korean.go.kr/openapi/service/PureKoreanService/getPureKoreanInfo?serviceKey=/24OQpkS/C2/MNknFON5Bic5EW6OmidfNP6ymQ1h/e3W6ap2IwBYIm50OIe5BwbTHWZ/mM4IVNMDcQF6XyPdhg==&id=";
	
	private Handler mHandler;
	
	private static final int MAX = 20;

	public KoreaDictionaryXMLParser(Handler handler) {
		this.mHandler = handler;
	}

	@Override
	public void run() {
		boolean inItem = false;
		boolean inContent = false;
		boolean inId = false;
		boolean inTitle = false;

		try {
			int randomID = (int)(Math.random() * MAX + 3);
			
			Log.i(TAG, randomID + "");
			
			mUrl += randomID;
			URL url = new URL(mUrl);

			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(url.openStream(), "utf-8");

			int eventType = parser.getEventType();
			
			KoreanDictionary koreaDictionary = new KoreanDictionary();
			
			//return END_DOCUMENT, START_DOCUMENT…
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_TAG:
					Log.i(TAG, "here");
					if (parser.getName().equals("item")) {
						inItem = true;
					} else if (parser.getName().equals("content")) {
						inContent = true;
					} else if (parser.getName().equals("id")) {
						inId = true;
					} else if (parser.getName().equals("title")) {
						inTitle = true;
					} 
					break;
				case XmlPullParser.END_TAG:
					if (parser.getName().equals("item")) {
						inItem = false;
					} else if (parser.getName().equals("content")) {
						inContent = false;
					} else if (parser.getName().equals("id")) {
						inId = false;
					} else if (parser.getName().equals("title")) {
						inTitle = false;
					} 
					break;
				case XmlPullParser.TEXT:
					if (inItem) {
						if (inContent){
							String itemName = parser.getText();
							String[] strs = itemName.split("¶");
							itemName = strs[0] + "\n 예] " + strs[1];
							koreaDictionary.setContent(itemName);
							Log.i(TAG, "content : " + itemName);
						} else if (inId){
							String itemName = parser.getText();
							koreaDictionary.setId(itemName);
							Log.i(TAG, "id : " + itemName);
						} else if (inTitle){
							String itemName = parser.getText();
							koreaDictionary.setTitle(itemName);
							Log.i(TAG, "title : " + itemName);
						}
					}
					break;
				}
				eventType = parser.next();
			}

			Message msg = mHandler.obtainMessage(ChoiceActivity.KOREA_DICTIONARY, koreaDictionary);
			mHandler.sendMessage(msg);
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
		}
	}
}